package cn.parus.auth.controller.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

/**
 * @Author: Axel
 * @Description: TODO 配置WebClient
 **/
@Configuration
public class WebClientConfig {

    /**
     * OPA 访问地址
     */
    @Value("${mini-gateway.config.opa_url}")
    private String OPA_URL;

    /**
     * 针对Header来说，不同的请求所带的Contente-Type不同
     * 因此需要动态，后续调整
     * @return
     */
    @Deprecated
    @Bean
    public WebClient opaWebClient() {
        return WebClient.builder()
                .baseUrl(OPA_URL)
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .clientConnector(new ReactorClientHttpConnector(
                        HttpClient.create()
                                .responseTimeout(Duration.ofSeconds(10))
                ))
                .build();
    }
}
